'use strict';
import Base from './base.js';
//美易通规划类控制器请求方法
module.exports = class extends Base {
//根据开始时间和结束时间查找规划数据
async getplaninfomonthAction(){
    let start_time = this.get('start_time');
    let end_time = this.get('end_time');
    let user_id = this.get('user_id');
    let info = await this.model().query(' SELECT og.name,og.headurl,og.sex,po.arrivetime,po.amount,po.id FROM `think_plan_order` po LEFT JOIN `think_or_guest` og ON po.guestid = og.id WHERE po.userid = '+user_id+' AND po.arrivetime >= '+start_time+' AND po.arrivetime < '+end_time+' ');
    return think.isEmpty(info)?this.json({'code':"200",'msg':"未找到数据...",'data':''}):this.json({'code':"100",'msg':"添加成功...",'data':info});
}
//获取前端提交的规划数据
async postaddplaninfoAction(){
    let post_info = this.post('post_info');
    let order_info = this.post('order_info');
    let addinfo = JSON.parse(post_info);
    let add_order = JSON.parse(order_info);
    let add_order_info = [];
    addinfo['addtime'] = Math.round(Date.now()/1000);
    let info = await this.model('plan_order').add(addinfo);
    for(let i = 0 ; i < add_order.length ; i++ ){
        add_order_info.push({'orderid':info,'goodsid':add_order[i].id,'goodsname':add_order[i].name,'num':add_order[i].num,'price':add_order[i].price,'amount':add_order[i].amount*add_order[i].num,'type':add_order[i].type});
    }

    let add_res = await this.model('plan_order_info').addMany(add_order_info);
    return think.isEmpty(add_res)?this.json({'code':"200",'msg':"添加失败...",'data':''}):this.json({'code':"100",'msg':"添加成功...",'data':add_res});
} 
//根据规划订单order的id 获取产品详细列表
async getplanorderinfolistAction(){
    let orderid = this.get('orderid');
    let info = await this.model().query( 'SELECT * FROM `think_plan_order_info` WHERE orderid = '+orderid+' ' );
    return think.isEmpty(info)?this.json({'code':"200",'msg':"未找到数据...",'data':''}):this.json({'code':"100",'msg':"添加成功...",'data':info});
}
//根据顾问的所属部门查找当前小组下的员工规划信息
async getplaninfoxiaozuAction(){
    let start_time = this.get('start_time');
    let end_time = this.get('end_time');  
    let departmentid = this.get('departmentid');
    let info = await this.model().query(' SELECT au.id,au.name,IFNULL(A.amount,0) as amount,au.headurl,au.sex FROM `think_or_app_user` au LEFT JOIN  (SELECT au.id as userid,IFNULL(sum(po.amount),0) as amount FROM `think_plan_order` po LEFT JOIN `think_or_app_user` au ON po.userid = au.id WHERE au.xiaozuid = '+departmentid+' AND au.ismanage <> 1 AND po.arrivetime >= '+start_time+' AND po.arrivetime < '+end_time+' GROUP BY po.userid) as A  ON au.id = A.userid WHERE au.xiaozuid = '+departmentid+' AND au.ismanage <> 1 ');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"未找到数据...",'data':''}):this.json({'code':"100",'msg':"添加成功...",'data':info});
}
//根据开始时间和结束时间查找规划数据
async getplaninfomonthAction(){
    let start_time = this.get('start_time');
    let end_time = this.get('end_time');
    let user_id = this.get('user_id');
    let info = await this.model().query(' SELECT og.name,og.sex,og.headurl,po.arrivetime,po.amount,po.id FROM `think_plan_order` po LEFT JOIN `think_or_guest` og ON po.guestid = og.id WHERE po.userid = '+user_id+' AND po.arrivetime > '+start_time+' AND po.arrivetime < '+end_time+' ');
    return think.isEmpty(info)?this.json({'code':"200",'msg':"未找到数据...",'data':''}):this.json({'code':"100",'msg':"添加成功...",'data':info});
}
//根据当前提供的部门id 获取经理下的所有小组 和小组下的员工规划
async getplaninfojingliAction(){
    let start_time = this.get('start_time');
    let end_time = this.get('end_time'); 
    let departmentid = this.get('departmentid');
    let info = await this.model().query(' SELECT A.id as departmentid,A.name,IFNULL(B.amount,0) as amount FROM (SELECT id,name FROM `think_or_department` WHERE pid = '+departmentid+' ) as A LEFT JOIN  (SELECT au.xiaozuid,sum(po.amount) as amount FROM `think_plan_order` po LEFT JOIN `think_or_app_user` au ON po.userid = au.id LEFT JOIN `think_or_department` od ON au.xiaozuid = od.id WHERE po.arrivetime >= '+start_time+' AND po.arrivetime < '+end_time+' AND au.bumenid = '+departmentid+' AND au.xiaozuid <> 0 GROUP BY au.xiaozuid) as B ON A.id = B.xiaozuid ');
    return think.isEmpty(info)?this.json({'code':"200",'msg':"未找到数据...",'data':''}):this.json({'code':"100",'msg':"添加成功...",'data':info});
}
//根据当前提供的部门id 获取店长下的所有部门的员工规划
async getplaninfodianzhangAction(){
    let start_time = this.get('start_time');
    let end_time = this.get('end_time');  
    let departmentid = this.get('departmentid');
    let info = await this.model().query(' SELECT A.id as departmentid,A.name,IFNULL(B.amount,0) as amount FROM (SELECT id,name FROM `think_or_department` WHERE pid = '+departmentid+' ) as A LEFT JOIN  (SELECT au.bumenid,sum(po.amount) as amount FROM `think_plan_order` po LEFT JOIN `think_or_app_user` au ON po.userid = au.id LEFT JOIN `think_or_department` od ON au.bumenid = od.id WHERE po.arrivetime >= '+start_time+' AND po.arrivetime < '+end_time+' AND au.shopid = '+departmentid+' AND au.bumenid <> 0 GROUP BY au.bumenid) as B ON A.id = B.bumenid ');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"未找到数据...",'data':''}):this.json({'code':"100",'msg':"添加成功...",'data':info});
  }
//获取部门规划
async getplaninfozongbuAction(){
    let start_time = this.get('start_time');
    let end_time = this.get('end_time');  
    let jsonshop = this.get('jsonshop');
    let info = await this.model().query(' SELECT A.id,A.name,IFNULL(B.amount,0) as amount FROM (SELECT id,name FROM `think_or_department` WHERE id in ('+jsonshop+')) as A LEFT JOIN (SELECT au.shopid,sum(IFNULL(po.amount ,0)) as amount FROM `think_or_app_user` au RIGHT JOIN `think_plan_order` po ON po.userid = au.id WHERE au.shopid in ('+jsonshop+') AND po.arrivetime > '+start_time+' AND po.arrivetime < '+end_time+' GROUP BY au.shopid ) as B ON A.id = B.shopid ');
    return think.isEmpty(info)?this.json({'code':"200",'msg':"未找到数据...",'data':''}):this.json({'code':"100",'msg':"添加成功...",'data':info});
}

//bottom
};